#include "glare.d3d11.ShaderResourceView.h"
#include "glare.d3d11.GpuResourceFactory.h"
#include "Glare.D3D11.Driver.h"
#include "Glare.D3D11.FormatMapping.h"

namespace Glare {

	D3D11ShaderResourceView::D3D11ShaderResourceView(D3D11GpuResourceFactory* factory, D3D11GpuResourceInterface* resource_interface, const ShaderResourceViewDescription& description)
		: ShaderResourceView(description)
		, _factory(factory)
		, _d3d_shader_resource_view(NULL)
	{
		D3D11Driver* d3d11_rs = static_cast<D3D11Driver*>(factory->GetDriver());

		D3D11_SHADER_RESOURCE_VIEW_DESC d3d11_srv_desc;
		d3d11_srv_desc.Format = D3D11FormatMapping::MapFormat(description.Format);
		d3d11_srv_desc.ViewDimension = (D3D11_SRV_DIMENSION)description.ViewDimension;
		memcpy(&d3d11_srv_desc.Buffer, &description.Resource, sizeof(ShaderResourceViewDescription::ResourceUnion));

		ID3D11Resource* d3d_resource = resource_interface->GetD3DResource();

		GLARE_THROW(d3d11_rs->GetD3DDevice()->CreateShaderResourceView(d3d_resource, &d3d11_srv_desc, &_d3d_shader_resource_view), "Error while creating D3D shader resource view");
	}

	D3D11ShaderResourceView::~D3D11ShaderResourceView()
	{
		GLARE_SAFE_RELEASE(_d3d_shader_resource_view, "Error while releasing D3D sampler state");
	}

	GpuResourceFactory* D3D11ShaderResourceView::GetFactory() const
	{
		return _factory;
	}

	ID3D11ShaderResourceView* D3D11ShaderResourceView::GetD3DShaderResourceView() const
	{
		return _d3d_shader_resource_view;
	}

	D3D11GpuResourceFactory* D3D11ShaderResourceView::GetD3DFactory() const
	{
		return _factory;
	}
}
